package cn.bearspark.article.provider.rpc;

import cn.bearspark.article.dto.article.*;
import cn.bearspark.article.provider.service.ArticleService;
import cn.bearspark.article.provider.service.CategoryService;
import cn.bearspark.article.provider.service.DraftService;
import cn.bearspark.article.provider.service.bo.Article4ModifyBO;
import cn.bearspark.article.provider.service.bo.ArticleCategoryBO;
import cn.bearspark.article.provider.service.bo.ArticleDeleteBO;
import cn.bearspark.article.provider.service.bo.ArticleDetailBO;
import cn.bearspark.article.rpc.ArticleRpc;
import cn.bearspark.common.enumeration.VisibilityEnum;
import cn.bearspark.common.util.ConvertBeanUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author f
 */
@Component
@DubboService
public class ArticleRpcImpl implements ArticleRpc {

    private final ArticleService articleService;
    private final CategoryService categoryService;
    private final DraftService draftService;

    public ArticleRpcImpl(ArticleService articleService,
                          CategoryService categoryService,
                          DraftService draftService) {
        this.articleService = articleService;
        this.categoryService = categoryService;
        this.draftService = draftService;
    }

    @Override
    public void publish(PublishArticleDTO publishArticleDTO) {
        // 增加分类的 全部专栏数 和 公开专栏数，如果增加失败，则直接返回
        long userId = publishArticleDTO.getUserId();
        int visibility = publishArticleDTO.getVisibility();
        List<Long> categoryIdList = publishArticleDTO.getCategoryIdList();
        if (!categoryService.incrArticleNum(userId, visibility, categoryIdList)) {
            return;
        }

        // 保存发布专栏的信息
        articleService.publish(publishArticleDTO);

        // 删除草稿
        Long draftId = publishArticleDTO.getDraftId();
        if (draftId != null) {
            draftService.delete(draftId, userId);
        }
    }

    @Override
    public Article4ModifyDTO get(long articleId, long userId) {
        // 获取专栏修改数据的业务数据，如果获取不到，说明没有权限，返回 null
        Article4ModifyBO articleDetailBO = articleService.get(articleId, userId);
        if (articleDetailBO == null) {
            return null;
        }

        // 填充分类信息
        Article4ModifyDTO article4ModifyDTO = ConvertBeanUtil.convert(articleDetailBO, Article4ModifyDTO.class);
        article4ModifyDTO.setCategorySimpleDTOList(categoryService.listSimpleByCategoryId(
                articleDetailBO.getCategoryIdList()));
        return article4ModifyDTO;
    }

    @Override
    public void modify(ModifyArticleDTO modifyArticleDTO) {
        long articleId = modifyArticleDTO.getArticleId();
        long userId = modifyArticleDTO.getUserId();

        // 获取原先专栏的分类数据，如果获取不到，说明没有权限，直接返回；如果专栏的可见性为 不可见，也直接返回
        ArticleCategoryBO acBO = articleService.getCategoryInfo(articleId, userId);
        if (acBO == null || acBO.getVisibility() == VisibilityEnum.INVISIBLE.getCode()) {
            return;
        }

        // 修改分类的专栏数，如果修改失败，则直接返回
        List<Long> oldCategoryIdList = acBO.getCategoryIdList();
        if (!categoryService.updateArticleNum(userId, acBO.getVisibility(), modifyArticleDTO.getVisibility(),
                oldCategoryIdList, modifyArticleDTO.getCategoryIdList())) {
            return;
        }

        // 修改专栏的数据
        articleService.modify(modifyArticleDTO, oldCategoryIdList);
    }

    @Override
    public Long delete(long articleId, long userId) {
        // 删除专栏的信息，如果删除失败，则直接返回
        ArticleDeleteBO articleDeleteBO = articleService.delete(articleId, userId);
        if (articleDeleteBO == null) {
            return null;
        }

        // 减少分类的 全部专栏数 和 公开专栏数
        categoryService.decrArticleNum(userId, articleDeleteBO.getVisibility(), articleDeleteBO.getCategoryIdList());

        return articleDeleteBO.getHappeningId();
    }

    @Override
    public ArticleDetailDTO detail(long articleId, long userId) {
        // 获取专栏详情的业务数据，如果获取不到，说明没有权限，返回 null
        ArticleDetailBO articleDetailBO = articleService.detail(articleId, userId);
        if (articleDetailBO == null) {
            return null;
        }

        // 填充分类信息
        ArticleDetailDTO articleDetailDTO = ConvertBeanUtil.convert(articleDetailBO, ArticleDetailDTO.class);
        List<Long> categoryIdList = articleDetailBO.getCategoryIdList();
        if (categoryIdList != null && !categoryIdList.isEmpty()) {
            articleDetailDTO.setCategorySlimDTOList(categoryService.listSlimByCategoryId(categoryIdList));
        }
        return articleDetailDTO;
    }

    @Override
    public List<ArticleSimpleDTO> page(int pageNum, int pageSize, Long categoryId, long userId, long listedUserId) {
        return articleService.page(pageNum, pageSize, categoryId, userId, listedUserId);
    }

    @Override
    public void incrCollectNum(long articleId) {
        articleService.incrCollectNum(articleId);
    }

    @Override
    public void decrCollectNum(long articleId) {
        articleService.decrCollectNum(articleId);
    }

    @Override
    public void incrMeritNum(long articleId) {
        articleService.incrMeritNum(articleId);
    }

    @Override
    public void onArticleDeadline(List<String> infoList) {
        articleService.onArticleDeadline(infoList);
    }
}
